package cn.beyond.vehicleManagement.controller;

import cn.beyond.vehicleManagement.pojo.SysRole;
import cn.beyond.vehicleManagement.pojo.Vehicles;
import cn.beyond.vehicleManagement.service.RoleService;
import cn.beyond.vehicleManagement.service.VehicleService;
import cn.beyond.vehicleManagement.util.PageUtil;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.util.concurrent.CountDownLatch;

@WebServlet(urlPatterns = {"/roleServlet"})
public class RoleServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //定义全局变量
        RoleService service=new RoleService();
        String opr= req.getParameter("opr");
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        SysRole sysRole=new SysRole();
        Map<String,Object> map = new HashMap<>();
        PrintWriter out= resp.getWriter();
        List<SysRole> sysRoleList=new ArrayList<>();
        System.out.println(opr);
        switch (opr){
            case "addRole":
                boolean addflag= addRole(req,service);
                out.print(addflag);
                break;
            //删除角色
            case "deleteRole":
                int delId=Integer.parseInt(req.getParameter("Id"));
                boolean delflag=false;
                int delcount=service.deleteRole(delId,"id");
                System.out.println(delcount);
                if(delcount>0){
                    delflag=true;
                }
                out.print(delflag);
                break;
            case "updateRole":


                break;
            case "selectRole":

                break;
            case "listRole":
                //列举出角色列表
                int startRow= Integer.parseInt(req.getParameter("startRow"));
                int pageSize= Integer.parseInt(req.getParameter("pageSize"));
                int totals= PageUtil.getPageCount("sys_role");
                int totalpage=totals%pageSize==0?(totals/pageSize):(totals/pageSize+1);
                List<Integer>totalinfo=new ArrayList<>();
                totalinfo.add(totals);
                totalinfo.add(totalpage);
                sysRoleList= PageUtil.getPageUtil(startRow,pageSize,"sys_role",sysRole);
                List<Object>listListMap=new ArrayList<>();
                listListMap.add(totalinfo);
                listListMap.add(sysRoleList);
                //sysRoleList=service.selectUserByCondition(sysRole,map);
                String listJosn= JSON.toJSONString(listListMap);
                out.write(listJosn);
                map.clear();
                break;

        }
    }

    /**
     * 添加角色
     * @param req
     * @param service
     * @return
     */
    private boolean addRole(HttpServletRequest req, RoleService service) {
        String role_name=req.getParameter("role_name");
        String remark=req.getParameter("remark");
        SysRole role=new SysRole(null,role_name,0,remark,new Date());
        int coount=service.addRole(role);
        if(coount>0){
           return  true;
        }else {
            return  false;
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
